# -*- coding: utf-8 -*-
# @Time    : 2018/12/28 15:12
# @Author  : chen
# @Site    : 
# @File    : email_send.py
# @Software: PyCharm
# @Desc  : 描写一些邮件处理的代码

from random import Random

from django.core.mail import send_mail

from login.models import EmailVerifyRecord
from login_regs.settings import EMAIL_FROM


def send_register_email(email, send_type="register"):
    email_record = EmailVerifyRecord()
    email_record.code = generate_code(16)
    email_record.send_type = send_type
    email_record.email = email
    email_record.save()

    email_title = ""
    email_body = ""

    if send_type == "register":
        email_title = "注册激活链接"
        email_body = "请点击下面的连接进行激活：http://127.0.0.1:8000/active/{0}".format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:  # 如果发送成功 pass 没看懂
            pass

def generate_code(random_length=8):
    code = ''
    # 生成字符串的可选字符串
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars) - 1
    random = Random()
    for i in range(random_length):
        code += chars[random.randint(0, length)]
    return code
